//
//  KKCurrencyPickerView.m
//  FreightAssistant
//
//  Created by Mac mini on 15/12/29.
//  Copyright © 2015年 leo. All rights reserved.
//

#import "KKCurrencyPickerView.h"

#import "KKExchangeRateManager.h"

@interface KKCurrencyPickerView() {
    
    BOOL _isAppear;
    
    NSArray *_currencyItems;
}

@end

@implementation KKCurrencyPickerView

+ (instancetype)pickerView {
    
    static KKCurrencyPickerView *pickerView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        pickerView = [KKCurrencyPickerView new];
    });
    
    return pickerView;
}

- (instancetype)init {
    self = [super initWithFrame: CGRectMake(0, 0, CGRectGetWidth([[UIScreen mainScreen] bounds]), 220)];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _currencyItems = [[KKExchangeRateManager defaultManager] currencyItems];
        
        self.delegate = self;
    }
    return self;
}

- (void)setSelectedString:(NSString *)selectedString {
    
    if (selectedString != nil) {
        NSInteger index = [_currencyItems indexOfObject: selectedString];
        
        if (index < _currencyItems.count) {
            [self selectRow: index inComponent: 0 animated: false];
        }
    }
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    
    if ([self pointInside: point withEvent: event]) {
        return [super hitTest: point withEvent: event];
    }
    
    [self pickerViewDidDisappear];
    return nil;
}

#pragma mark - Public methods

- (void)show {
    [self showWithTitle: nil];
}

- (void)showWithTitle:(NSString *)title {
    
    if (_isAppear) {
        return;
    }
    
    
    [self setSelectedString: title];

    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    self.center = CGPointMake(CGRectGetWidth(window.bounds)/2, CGRectGetHeight(window.bounds) + CGRectGetHeight(self.bounds)/2);
    [window addSubview: self];
    
    __weak typeof(self) weakSelf = self;
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    [UIView animateWithDuration: 0.25 animations:^{
        
        weakSelf.center = CGPointMake(CGRectGetWidth(weakSelf.bounds)/2, CGRectGetHeight([[UIScreen mainScreen] bounds]) - CGRectGetHeight(weakSelf.bounds)/2);
    } completion:^(BOOL finished) {
        [[UIApplication sharedApplication] endIgnoringInteractionEvents];
        
        _isAppear = true;
    }];
}

#pragma mark - Private methods

- (void)pickerViewDidDisappear {
    
    if (!_isAppear) {
        return;
    }
    
    _isAppear = false;
    
    __weak typeof(self) weakSelf = self;
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    [UIView animateWithDuration: 0.25 animations:^{
        
        weakSelf.center = CGPointMake(CGRectGetWidth(weakSelf.bounds)/2, CGRectGetHeight([[UIScreen mainScreen] bounds]) + CGRectGetHeight(weakSelf.bounds)/2);
    } completion:^(BOOL finished) {
        [[UIApplication sharedApplication] endIgnoringInteractionEvents];
        [weakSelf removeFromSuperview];
    }];
}

#pragma mark - UIPickerView methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    return _currencyItems.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    return _currencyItems[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    NSString *selectedString = _currencyItems[row];
    if (_selectedBlock) {
        _selectedBlock (selectedString);
    }
}

@end
